package com.shipwe.loans.utils.helper

import com.shipwe.loans.utils.shared.ShipweSharedHelper
import com.shipwe.loans.utils.shared.ShipweSharedKey
import org.json.JSONObject

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/19 10:44
 * @Annotation：用户流程状态 帮助类
 */
object CustomProcessHelper {

    /**
     * 保存用户流程数据
     */
    fun saveAuthProcessData(data: JSONObject){
        ShipweSharedHelper.putShipweSharedData(ShipweSharedKey.SHIPWE_PROCESS, data.toString())
    }

    /**
     * 获取流程数据
     */
    fun getAuthProcessData(): JSONObject? {
        val dataString = ShipweSharedHelper.getShipweSharedString(ShipweSharedKey.SHIPWE_PROCESS)
        if (dataString.isEmpty()){
            return null
        }
        return JSONObject(dataString)
    }

    /**
     * 获取所有流程titleList
     */
    fun getAuthStepTitleList(): ArrayList<String> {
        val titleList = arrayListOf<String>()
        val data = getAuthProcessData() ?: return titleList

        val allStepTitles = data.optJSONArray("allStepTitles")
        if (allStepTitles != null) {
            for (i in 0..<allStepTitles.length()){
                titleList.add(allStepTitles.optString(i))
            }
        }
        return titleList
    }

    /**
     * 获取总流程步骤key
     */
    fun getAuthAllStepList(): ArrayList<String>{
        val allStepList = arrayListOf<String>()
        val data = getAuthProcessData() ?: return allStepList
        val allSteps = data.optJSONArray("allSteps")
        if (allSteps != null){
            for (i in 0..<allSteps.length()){
                allStepList.add(allSteps.optString(i))
            }
        }
        return allStepList
    }

    /**
     * 获取剩余未完成的流程列表
     */
    fun getAuthStepList(): ArrayList<String>{
        val stepList = arrayListOf<String>()
        val data = getAuthProcessData() ?: return stepList
        val steps = data.optJSONArray("steps")
        if (steps != null){
            for (i in 0..<steps.length()){
                stepList.add(steps.optString(i))
            }
        }
        return stepList
    }

    /**
     * 获取获取当前进行的步骤下标
     */
    fun getAuthStepIndex(): Int{
        var stepIndex = getAuthAllStepList().size
        if (getAuthStepList().size == 0){
            //所有步骤已完成
            return stepIndex
        }
        val curtStep = getAuthStepList()[0]
        for (i in 0..<stepIndex){
            if (curtStep == getAuthAllStepList()[i]){
                stepIndex = i
                break
            }
        }
        return stepIndex
    }

    /**
     * 获取订单状态
     */
    fun getAuthOrderStatus(): String{
        val data = getAuthProcessData() ?: return ""
        return data.optString("orderStatus","")
    }

    /**
     * 获取申请状态
     */
    fun getAuthApplyStatus(): String{
        val data = getAuthProcessData() ?: return ""
        return data.optString("applyStatus","")
    }

    /**
     * 获取状态 string
     */
    fun getAuthStatusRemark(): String{
        //申请状态
        val applyStatus = getAuthApplyStatus()
        //订单状态
        val orderStatus = getAuthOrderStatus()
        val isReviewing = ProductStatusHelper.StatusRepository(applyStatus,orderStatus).isReviewing()
        if (isReviewing){
            return "LoanAudit"
        }
        val isDisbursing = ProductStatusHelper.StatusRepository(applyStatus,orderStatus).isDisbursing()
        if (isDisbursing){
            return "LoanLending"
        }
        val isRejected = ProductStatusHelper.StatusRepository(applyStatus,orderStatus).isRejected()
        if (isRejected){
            return "LoanRejected"
        }
        val isRepay = ProductStatusHelper.StatusRepository(applyStatus,orderStatus).isRepayNow()
        if (isRepay){
            return "LoanRepay"
        }
        return "None"
    }

    /**
     * 是否是可以修改手机号
     */
    fun isMobileEditFlag(): Boolean{
        val data = getAuthProcessData() ?: return false
        return data.optString("mobileEditFlag") == "YES"
    }

    /**
     * 是否开放WhatsApp手机校验方式  YES、NO
     */
    fun isCheckWhatsApp(): Boolean{
        val data = getAuthProcessData() ?: return false
        return data.optString("whatsappFlag") == "YES"
    }

    /**
     * 是否开放SMS 手机校验方式  YES、NO
     */
    fun isCheckSMS(): Boolean{
        val data = getAuthProcessData() ?: return false
        return data.optString("smsFlag") == "YES"
    }

    /**
     * 获取可再次申请时间
     */
    fun getAuthApplyAgainDate(): String{
        val data = getAuthProcessData() ?: return ""
        return data.optString("againApplyDate")
    }

}